#include <keyboard.h>
#include <ports.h>
#include <printThings.h>
#include <sharedMem.h>

char stdIn[1024];

int aktStd = 0;

typedef enum {  keyA = 0x1E,
                keyB = 0x30,
                keyC = 0x2E,
                keyD = 0x20,
                keyE = 0x12,
                keyF = 0x21,
                keyG = 0x22,
                keyH = 0x23,
                keyI = 0x17,
                keyJ = 0x24,
                keyK = 0x25,
                keyL = 0x26,
                keyM = 0x32,
                keyN = 0x31,
                keyO = 0x18,
                keyP = 0x19,
                keyQ = 0x10,
                keyR = 0x13,
                keyS = 0x1F,
                keyT = 0x14,
                keyU = 0x16,
                keyV = 0x2F,
                keyW = 0x11,
                keyX = 0x2D,
                keyY = 0x2C,
                keyZ = 0x15,
                keyReturn = 0x1C
             } keys;

char convertScanCodeToKey(unsigned char scancode) {
    switch (scancode) {
        case keyA: {
            return 'a';
        }
        case keyB: {
            return 'b';
        }
        case keyC: {
            return 'c';
        }
        case keyD: {
            return 'd';
        }
        case keyE: {
            return 'e';
        }
        case keyF: {
            return 'f';
        }
        case keyG: {
            return 'g';
        }
        case keyH: {
            return 'h';
        }
        case keyI: {
            return 'i';
        }
        case keyJ: {
            return 'j';
        }
        case keyK: {
            return 'k';
        }
        case keyL: {
            return 'l';
        }
        case keyM: {
            return 'm';
        }
        case keyN: {
            return 'n';
        }
        case keyO: {
            return 'o';
        }
        case keyP: {
            return 'p';
        }
        case keyQ: {
            return 'q';
        }
        case keyR: {
            return 'r';
        }
        case keyS: {
            return 's';
        }
        case keyT: {
            return 't';
        }
        case keyU: {
            return 'u';
        }
        case keyV: {
            return 'v';
        }
        case keyW: {
            return 'w';
        }
        case keyX: {
            return 'x';
        }
        case keyY: {
            return 'y';
        }
        case keyZ: {
            return 'z';
        }
        case keyReturn: {
            return '\n';
        }
    }
    return ' ';
}

bool canWrite(unsigned char in) {
    if ((in & INPUT_BUFFER) == 0)
        return TRUE;
    return FALSE;
}

bool canRead(unsigned char in) {
    if ((in & OUTPUT_BUFFER) == 1)
        return TRUE;
    return FALSE;
}

bool keyboardIsOkay(unsigned char in) {
    if ((in & SELFCHECK) == 1)
        return TRUE;
    return FALSE;
}

void sendCommandToKeyboard(unsigned char cmd) {
    while ((inb(0x64) & 0x2)) {}
    outb(BUF_PORT, cmd);
}

void clearKeyboardBuffer(void) {
    while (canRead(inb(0x64)))
        inb(0x60);
}

void initKeyboard(void) {
    /*clear the keyboardbuffer*/
    clearKeyboardBuffer();

    /*aktivate keyboard*/
    sendCommandToKeyboard(0xF4);

    sendCommandToKeyboard(0xED);
    sendCommandToKeyboard(0x7);
}

void handleKeyboardInterrupt() {
    /*get the keycode*/
    unsigned char keyCode = inb(0x60);

    /*sendet mir die tastur ein: is alles ok dude?*/
    if (keyCode == ACKNOWLEDGE) {
        /*ka was man dann tut soll...*/
    } else {
        /*pruefen ob taste gedrueckt oder losgelassen*/
        if ((keyCode & KEY_PRESS) == 0) {/*gedrückt*/
            addKeyToStdIn(convertScanCodeToKey(keyCode));
        } else {/*losgelassen*/
        }
    }

    int buf = 0;
    buf = keyCode;

    //printf("aktualKeycode: %d\n", buf);
    clearKeyboardBuffer();
}

